// 导入首页
import React from "react";
import Index from "../page/index/index";
// 导入登录页
import Login from "../page/login";
import { Navigate } from "react-router-dom";
import {  RoleContextType, useRoleContext } from "../provider/tsx";
import App from "../App";
import ProjectList from "../layout/ProjectList";
import { Project } from "../page/project/project";
import Env from "../page/env/env";
import {Token} from "../page/token/token";
import { AuthSessionData } from "../types";
import { FileTextOutlined, HomeOutlined } from "@ant-design/icons";
import { Account } from "../page/account/account";

// 配置路由映射 （不同的路由对应渲染不同的页面组件）
const Auth = ({ children }) => {
  const ss  = useRoleContext();

  const auth = ss.getSessionData();
  console.log("authauth",auth)
    const  role = auth?.role  ?? '';
    const isAdmin = role == "admin"; // 假设这里有一个逻辑判断用户是否登录
    const isGuest = ss == null || auth == null || auth.role == "guest" || !auth.token; // 假设这里有一个逻辑判断用户是否登录
    if (isGuest) {
      // 如果用户未登录，跳转到登录页面
      return <Navigate to="/login" />;
    }
    return <>{children}</>;
  };
  type RouterInfo = {path:string,role?: string,title?: string,type?:string, element:React.ReactNode, icon?:React.ReactNode}

const router  = [
  {
    path: "/",
    role: "common",
    title: "配置管理",
    type: "sidebar",
    icon: <FileTextOutlined className="mr-4 text-gray-500" />,
    element:  <Auth> <Index /></Auth>,
  },
 
  {
    path: "/admin/project",
    role: "admin",
    title:"项目管理",
    type: "sidebar",

    icon: <FileTextOutlined className="mr-4 text-gray-500" />,
    element:  <Auth> <App >  <Project></Project></App></Auth>,
  },
  {
    path: "/admin/env",
    role: "admin",
    title: "环境管理",
    type: "sidebar",
    icon: <FileTextOutlined className="mr-4 text-gray-500" />,
    element:  <Auth> <App >  <Env></Env></App></Auth>,
  },
  {
    path: "/admin/token",
    title: "Token管理",
    role: "admin",
    type: "sidebar",
    icon: <FileTextOutlined className="mr-4 text-gray-500" />,
    element:  <Auth> <App > <Token></Token></App></Auth>,
  },
  {
    path: "/admin/account",
    title: "账号管理",
    role: "admin",
    type: "sidebar",
    icon: <FileTextOutlined className="mr-4 text-gray-500" />,
    element:  <Auth> <App > <Account /></App></Auth>,
  },
  {
    path: "/login",
    role: "guest",
    type: "",
    element:   <Login />,
  },
 
]
export const getFilterRouter = (sd:AuthSessionData):Array<RouterInfo>=>{
  let rl = router.filter(x=>{
    if(sd.role == "admin"){
      return true;
    }
    return ["guest",sd.role].indexOf(x.role) > -1
  });

  return rl;
}  
 
export default router
